const path = require('path');
const express = require('express');
const app = express()
const got = require('got');
app.use(express.json());
app.use(express.urlencoded({
	extended: false
}));

app.all('*', function(req, res, next) {
	res.header("Access-Control-Allow-Origin", "*");
	res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
	next();
});
app.use('/', (req, res) => {
	const query = req.query
	const url = query.url
	const reg = query.reg
	let data = [],
		arr = [];
		let reg1 = new RegExp(reg, "gm");
		_get(url, reg1).then(e=>{
			console.log(e);
			if(!reg){
				res.json({
					data: e
				})
				return 
			}
			e.replace(reg1, (...g) => {
				const l = g.length - 3;
				for (let i = 1; i <= l; i++) {
					data.push(g[i])
					if (i % l == 0) {
						arr.push(data)
						data = []
					}
				}
			})
			res.json({
				data: arr
			})
		})
})
app.listen(3000, () => {
	console.log("run 3000");
})
async function _get(url, reg1) {
	const response = await got(url);
	const str = response.body;
	return str
}
